#include <vector>
#include <iostream>

using namespace std;

class Solution {
private:
    int height;
    int width;
    vector<vector<char>>* grid;
    void search(int i, int j) {
        if (i == height || j == width || i < 0 || j < 0 || grid->at(i).at(j) == 0) {
            return;
        }
        if (grid->at(i).at(j) == '1') {
            (*grid)[i][j] = 0;
            search(i + 1, j);
            search(i, j + 1);
            search(i - 1, j);
            search(i, j - 1);
        }
    }
public:
    int numIslands(vector<vector<char>>& grid) {
        if (grid.size() == 0 || grid[0].size() == 0) {
            return 0;
        }
        this->grid = &grid;
        height = grid.size();
        width = grid[0].size();
        int count = 0;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (grid[i][j] == '1') {
                    count++;
                    search(i, j);
                }
            }
        }
        return count;
    }
};

int main() {
    vector<vector<char>> input = {{'1', '1', '1'},
                                  {'0', '1', '0'},
                                  {'1', '1', '1'},};
    Solution solution;
    int output = solution.numIslands(input);
    cout << output << endl;
    return 0;
}
